package ai.people.platform.information.converter;

import ai.people.netmon.framework.domain.archives.dto.InformationThirdPartySearchReqDTO;
import ai.people.netmon.framework.domain.archives.dto.ThirdPartyInformationSearchRespDTO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * foza第三方数据提供商请求响应param转换器
 *
 * @author LiGuanNan
 * @date 2022/6/9 11:47
 */
@Component("fozaReqRespParamConverter")
public class FozaReqRespParamConverter implements StandardConverter {

    @Override
    public String searchRequestParamConvert(InformationThirdPartySearchReqDTO reqParam) {
        JSONObject data = new JSONObject();
        data.put("keyword", StringUtils.isNotBlank(reqParam.getKeyword()) ? reqParam.getKeyword() : "");
        data.put("searchType", reqParam.getSearchType() == null ? 1 : reqParam.getSearchType());
        data.put("platforms", "[]");
        data.put("startTime", 1276155472);
        data.put("endTime", System.currentTimeMillis() / 1000);
        data.put("pageNo", reqParam.getPageNo());
        data.put("pageSize", reqParam.getPageSize());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("version", "");
        jsonObject.put("data", data);
        return jsonObject.toJSONString();
    }

    @Override
    public List<ThirdPartyInformationSearchRespDTO> searchResponseParamConvert(JSONObject dataJson) {
        List<ThirdPartyInformationSearchRespDTO> list = Lists.newArrayList();
        JSONArray jsonArray = dataJson.getJSONArray("list");
        jsonArray.forEach(row -> {
            JSONObject rowData = (JSONObject) row;
            if(rowData != null && rowData.getJSONObject("fields") != null){
                LinkedHashMap<String, Object> map = Maps.newLinkedHashMap();
                map.put("平台", rowData.getJSONObject("fields").getString("platform"));
                map.put("邮箱", rowData.getJSONObject("fields").getString("mail"));
                map.put("手机号", rowData.getJSONObject("fields").getString("phone"));
                map.put("姓名", rowData.getJSONObject("fields").getString("name"));
                ThirdPartyInformationSearchRespDTO dto = ThirdPartyInformationSearchRespDTO.builder()
                        .dataId(rowData.getString("dataId"))
                        .weight(rowData.getInteger("weight"))
                        .fields(map)
                        .build();
                list.add(dto);
            }
        });
        return list;
    }

    @Override
    public String detailRequestParamConvert(String dataId) {
        JSONObject data = new JSONObject();
        data.put("dataId", dataId);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("version", "");
        jsonObject.put("data", data);
        return jsonObject.toJSONString();
    }

    @Override
    public Map<String, Object> detailResponseParamConvert(JSONObject fields) {
        LinkedHashMap<String, Object> map = Maps.newLinkedHashMap();
        map.put("平台", fields.getString("platform"));
        map.put("邮箱", fields.getString("mail"));
        map.put("手机号", fields.getString("phone"));
        map.put("姓名", fields.getString("username"));
        map.put("账号id", fields.getString("accountId"));
        map.put("昵称", fields.getString("nickName"));
        map.put("性别", fields.getString("gender"));
        map.put("注册时间", fields.getString("registerTime"));
        map.put("地址", fields.getString("country") + fields.getString("province") + fields.getString("city"));
        map.put("密码", fields.getString("password"));
        map.put("婚姻状况", fields.getString("marriageState"));
        map.put("关注数量", fields.getString("followedCount"));
        map.put("粉丝数量", fields.getString("fansCount"));
        map.put("工作经历", fields.getString("workExperience"));
        map.put("教育经历", fields.getString(" educationalExperience"));
        map.put("描述", fields.getString("description"));
        JSONArray extField = fields.getJSONArray("extendtion");
        extField.forEach(o->{
            JSONObject e = (JSONObject) o;
            if(StringUtils.isNotBlank(e.getString("l"))){
                if(!e.getString("l").equalsIgnoreCase("端口")){
                    map.put(e.getString("l"),e.getString("v"));
                }
            }
        });
        return map;
    }
}
